package com.example.fengyungo.utils;

import android.Manifest;
import android.content.pm.PackageManager;
import android.util.Log;
import android.widget.Toast;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentActivity;

import com.example.fengyungo.R;

/**
 * 权限相关工具类
 * @author 徐
 */
public class PermissionUtil {
    /**
     * 获取权限
     */
    public static void getPermission(FragmentActivity fragmentActivity) {
        // 动态获取权限
        try {
            //需要的权限
            String[] permArr = {
                    Manifest.permission.WRITE_EXTERNAL_STORAGE
            };
            boolean needReq = false;
            for (int i = 0; i < permArr.length; i++) {
                if (ContextCompat.checkSelfPermission(fragmentActivity, permArr[i]) != PackageManager.PERMISSION_GRANTED) {
                    needReq = true;
                    break;
                }
            }
            if (needReq) {
                ActivityCompat.requestPermissions(fragmentActivity, permArr, 1);
            }
        } catch (Exception e) {
            Toast.makeText(fragmentActivity, fragmentActivity.getString(R.string.get_permission_error), Toast.LENGTH_SHORT).show();
        }
    }
}
